package exchange;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class Consumer2 {

	private static final String EXCHANGE_NAME = "logs";
	
	public static void main(String[] args) {
		try {
			ConnectionFactory factory = new ConnectionFactory();
			factory.setHost("localhost");
			Connection connect = factory.newConnection();
			Channel channel = connect.createChannel();
			channel.exchangeDeclare(EXCHANGE_NAME,"fanout");
			String queueName = channel.queueDeclare().getQueue();
			System.out.println("queueName=========="+queueName);
			channel.queueBind(queueName,EXCHANGE_NAME,"*");
			
			Consumer consumer = new DefaultConsumer(channel) {

				@Override
				public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties,
						byte[] body) throws IOException {
//					super.handleDelivery(consumerTag, envelope, properties, body);
					String message = new String(body,"utf-8");
					 System.out.println(" consumer2 Received '" + message + "'");
				}
				
			};
			channel.basicConsume(queueName,true,consumer);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TimeoutException e) {
			e.printStackTrace();
		}
	}
}
